﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar.SuperGrid;
using AppCommon.Controls;
using SyncLib.Client;
using AppCommon;
using SyncApplication.Business;
using SyncLib;

namespace SyncApplication.Controls
{
    public partial class ChoiceScope4Sync : UserControlBase
    {
        public ChoiceScope4Sync()
        {
            InitializeComponent();
            historySyncTableAdapter.Connection.ConnectionString = CommonVariable.SqlConnectionString;
            if (!DesignMode)
            {
                GridMain.PrimaryGrid.Columns["Result"].RenderType = typeof(SyncApplication.Controls.ClientConfig.MyGridImageControl);
                GridDateTimeInputEditControl c = (GridDateTimeInputEditControl)GridMain.PrimaryGrid.Columns["SyncDate"].RenderControl;
                c.Format = DevComponents.Editors.eDateTimePickerFormat.Custom;
                c.CustomFormat = "dd/MM/yyyy HH:mm:ss";
            }
        }
        public void DataBind()
        {
            historySyncTableAdapter.Fill(syncHistoryDS.HistorySync);
        }

        private void btnSyncAll_Click(object sender, EventArgs e)
        {
            if (ClientSyncContext.IsRunning)
            {
                FrmNotify.Current.SyncAppNotify.ShowBalloonTip(100, "Keeper Sync", "Quá trình đồng bộ đang chạy\n Không thể thực hiện thêm", ToolTipIcon.Error);
                return;
            }
            else
            {
              //  SyncDialog.Current.DetachDataSource();
                ClientSyncContext.SyncDS.TaskSyncList.Clear();
                ClientSyncContext.SyncDS.AcceptChanges();
                for (int i = 0; i < GridMain.PrimaryGrid.Rows.Count; i++)
                {
                    GridRow currentRow = (GridRow)GridMain.PrimaryGrid.Rows[i];
                    if (currentRow.Checked)
                    {
                        DataRowView rowView = currentRow.DataItem as DataRowView;
                        SyncHistoryDS.HistorySyncRow row = rowView.Row as SyncHistoryDS.HistorySyncRow;
                        ClientSyncContext.AddScopeToTaskList(row.CopeName);
                    }
                }
             //  SyncDialog.Current.RefreshDatasource();
                SynchronizeHandle handle = new SynchronizeHandle();
                handle.ManualSynchronize();
            }
        }

        private void btniSyncChoiced_Click(object sender, EventArgs e)
        {
            btnSyncAll_Click(sender, e);
        }

        private void btniSyncAll_Click(object sender, EventArgs e)
        {
            if (ClientSyncContext.IsRunning)
            {
                FrmNotify.Current.SyncAppNotify.ShowBalloonTip(100, "Keeper Sync", "Quá trình đồng bộ đang chạy\n Không thể thực hiện thêm", ToolTipIcon.Error);
                return;
            }
            else
            {
                ClientSyncContext.SyncDS.TaskSyncList.Clear();

                for (int i = 0; i < GridMain.PrimaryGrid.Rows.Count; i++)
                {
                    GridRow currentRow = (GridRow)GridMain.PrimaryGrid.Rows[i];

                    DataRowView rowView = currentRow.DataItem as DataRowView;
                    SyncHistoryDS.HistorySyncRow row = rowView.Row as SyncHistoryDS.HistorySyncRow;
                    ClientSyncContext.AddScopeToTaskList(row.CopeName);

                }
                SynchronizeHandle handle = new SynchronizeHandle();
                handle.ManualSynchronize();
            }
        }

        private void ChoiceScope4Sync_Load(object sender, EventArgs e)
        {

        }


    }
}
